 /**
 * zmn配件模块-配件列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author DaiZhichao
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function () {
    window.$ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let layedit = layui.layedit;
    let laydate = layui.laydate;

    let hqPurchasePriceStr = permission.showHq ? {field: 'hqPurchasePriceStr', title: '总公司采购单价',width: 120,  align: 'center'} : {hide: true};
    let subPurchasePriceStr = permission.showSub ? {field: 'subPurchasePriceStr', title: '子公司采购单价', width: 120, align: 'center'} : {hide: true};

    let cols = [
        [
            {type: 'checkbox', fixed: 'left', width: 50, align: 'center'},
            // {type: 'numbers', title: '序号', width: 40, align: 'center'},
            {field: 'skuId', title: 'ID', width: 80, fixed: 'left', align: 'center', unresize: true, sort: true},
            {field: 'name', title: '配件名称',width: 240, fixed: 'left', align: 'center',templet:(d)=>{
                if (d.name) {
                    return d.name;
                } else {
                    return "--"
                }
                }},
            {field: 'categOneName', width: 120, title: '配件一级分类', align: 'center', sort: true},
            {field: 'categName', width: 120, title: '配件二级分类', align: 'center', sort: true},
            {field: 'brandName', width: 120, title: '配件品牌', align: 'center', sort: true},
            {field: 'unit', title: '单位', width: 80, align: 'center'},
            {
                title: '图片', width: 80, align: 'center', templet: (d) =>{
                    let imglist = [d.imgSrc,d.imgSrc2,d.imgSrc3,d.imgSrc4].filter(value => !zmnUtil.isEmptyNull(value));
                    return (imglist.length > 0) ? "<a class='layui-btn layui-btn-xs picLayer' data-id='" + d.layIndex + "'>查看</a>" : "<p>暂无图片</p>";
                }
            },
            hqPurchasePriceStr,
            subPurchasePriceStr,
            {field: 'status', title: '状态', width: 80, align: 'center', unresize: true, templet: "#zmn-templet-status", sort: true},
            {field: 'factoryName', title: '厂家配件名称', width: 230, align: 'center',templet:(d)=>{
                    if (d.factoryName) {
                        return d.factoryName;
                    } else {
                        return "--"
                    }
                }},
            {field: 'packSpec', title: '包装规格', width: 100, align: 'center',templet:(d)=>{
                    if (d.packSpec) {
                        return d.packSpec;
                    } else {
                        return "--"
                    }
                }},
            {fixed: 'right', title: '操作', width: 80, align: 'center', toolbar: '#zmn-part-table-bar'}
        ]
    ];

    let tableRender = function tableRender(data) {
        zmnLayUtil.layTableRender(table,cols,"zmn-part-table",data,"/part/ajax/list.action");
    };

    let tableReload = function () {
        table.reload("zmn-part-table");
    };

    tableRender();


    $("body").on("click mouseover", ".picLayer", event => {
        let index = $(event.target).data("id");
        picLayerRender(layui.table.cache["zmn-part-table"][index]);
    })

    let picLayerRender = (part) => {
        let imgList = [part.imgSrc, part.imgSrc2, part.imgSrc3, part.imgSrc4]
            .filter(value => !zmnUtil.isEmptyNull(value))
    .map(value => {
            return {
                alt: part.fullName,
                pid: part.skuId, //图片id
                src: `${value}?t=${Math.random()}`, //原图地址
                // thumb: value //缩略图地址
            }
        });
        layer.photos({
            photos: {
                "title": "", //相册标题
                "id": 123, //相册id
                "start": 0, //初始显示的图片序号，默认0
                "data": imgList
            }
            , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
        });
    }

    //监听表单提交
    form.on('submit(query-submit)', function (data) {
        zmnMothedUtil.deleteEmptyProperty(data.field);
        tableRender(data.field);
        return false;
    });


    // form.on('submit(query-export)', function (data) {
    //     zmnMothedUtil.deleteEmptyProperty(data.field);
    //     window.location.href = "/part/ajax/exportExcel.action" + zmnMothedUtil.serializeFormObj(data.field);
    //     tableRender(data.field);
    //     return false;
    // });

    // 监听导出按钮，并且读取查询form表单的值
    form.on('submit(query-export)', function () {
        var data ={};
         var t = $("#zmn-form-query").serializeArray();
         $ .each(t,function () {
             data[this.name]=this.value;
        });
        zmnMothedUtil.deleteEmptyProperty(data);
        window.location.href = "/part/ajax/exportExcel.action" + zmnMothedUtil.serializeFormObj(data);
        tableRender(data);
        return false;
    });

    // 分类下拉框联动
    zmnLayUtil.laySelectLinkage($, form, {
        option: {
            enableOnly: false
            , disableEffect: false
            , disableText: true
        }
        , selectId1: "categOneId"
        , selectUrl1: "/part/category/second/ajax/select.action"
        , selectId2: "categId"
    });

    //【表格右侧工具栏监听】
    table.on('tool(zmn-part-table)', function (obj) {
        let skuId = obj.data.skuId;
        if (obj.event === 'edit') {
            // zmnLayUtil.openIframe(parent.layui.layer,"修改配件");
            // window.location = "/part/addedit/" + skuId;
            return false;
        }
    });


    //【表格上方工具栏监听】
    table.on("toolbar(zmn-part-table)", function (obj) {

        if (obj.event === "add") {

        } else if (obj.event === "start"||obj.event === "stop") {
            let ids = table.checkStatus('zmn-part-table').data.map((item) => item.skuId);
            if (ids.length < 1) {

                layer.msg('请选择要操作的配件', {icon: 2});
                return false;
            }

            zmnLayUtil.tableEnableDisable($, obj, "/part/ajax/updateStatus.action", ids, function (data) {
                table.reload("zmn-part-table");
            });
        } else if (obj.event === "relation"){
            let ids = table.checkStatus('zmn-part-table').data.map((item) => item.skuId);
            if (ids.length != 1) {

                layer.msg('请选择单个配件', {icon: 2});
                return false;
            }
            zmnLayUtil.openIframeCustom(layer, "/part/relation/addEdit.action?skuId=" + ids[0], {area: ["100%", "100%"], title: "配件拆分管理"});
        } else if (obj.event === "unit"){
            zmnLayUtil.openIframeCustom(layer, "/part/unit/addEdit.action", {area: ["100%", "100%"]
                // , title: "配件单位比例"
            });
        }

        return false;

    });

    $("#rebuildIndex_part").on("click",function () {
        zmnLayUtil.ajaxResultDTOJson(layer,"/part/ajax/rebuildPartIndex.action")
    });

});